import sys
import os
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *

class Demo(QWidget):
    _step=0
    dialog=None
    customDialog=None
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        layout = QVBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignTop)
        
        btn=QPushButton('Show')
        btn.clicked.connect(self.Show)
        layout.addWidget(btn)

        btn=QPushButton('ShowModal')
        btn.clicked.connect(self.ShowModal)
        layout.addWidget(btn)

        btn=QPushButton('ShowAuto')
        btn.clicked.connect(self.ShowAuto)
        layout.addWidget(btn)

        btn=QPushButton('ShowCustom')
        btn.clicked.connect(self.ShowCustom)
        layout.addWidget(btn)

        self.timer=QTimer(self)        
        self.timer.setInterval(1000)
        self.timer.timeout.connect(self.OnTimer)
        self.setLayout(layout)
    def Show(self):        
        self.dialog=QProgressDialog('Normal Progress','取消',0,100,self)
        #self.dialog.setWindowTitle('Normal')
        #self.dialog.setLabelText('Normal Progress')
        #self.dialog.setCancelButtonText('取消')
        #self.dialog.setMinimum(0)
        #self.dialog.setMaximum(100)        

        self.dialog.canceled.connect(self.OnStop)
        self.timer.start()

    def ShowModal(self):        
        self.dialog=QProgressDialog('Modal Progress','停止',0,100,self)
        self.dialog.setWindowModality(Qt.WindowModality.WindowModal)
        self.dialog.canceled.connect(self.OnStop)
        self.timer.start()
    def ShowAuto(self):
        self.dialog=QProgressDialog('Auto Progress','停止',0,100,self)
        self.dialog.setWindowModality(Qt.WindowModality.WindowModal)
        self.dialog.setAutoClose(False)#取消满值自动关闭（默认100%自动关闭）
        self.dialog.setAutoReset(False)#取消自动重置（默认100%自动重置）
        self.dialog.canceled.connect(self.OnStop)
        self.timer.start()
    def ShowCustom(self):
        self.customDialog=QProgressDialog('Custom Progress','Stop',0,100,self)
        self.customDialog.setWindowModality(Qt.WindowModality.WindowModal)
        self.custombar=QProgressBar()
        self.custombar.setMinimum(0)
        self.custombar.setMaximum(100)
        self.custombar.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.customDialog.setBar(self.custombar)
        
        self.customDialog.canceled.connect(self.OnStop)
        self.timer.start()
    def OnTimer(self):
        if self.dialog is not None:
            self.dialog.setValue(self._step)        
        if self.customDialog is not None:
            self.custombar.setValue(self._step)
        self._step+=1
    def OnStop(self):
        self.timer.stop()
        self.dialog=None
        self.customDialog=None
        self._step=0
def runDemo(parent):
    wigdet =  Demo(parent)
    return wigdet